package org.come.bean;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.List;

import org.come.Frame.TestsmallmapJframe;
import org.come.model.Door;
import org.come.model.Gamemap;
import org.come.model.GamemapWorld;
import org.come.until.CutButtonImage;
import org.come.until.ScrenceUntil;
import org.come.until.Util;

import com.updateNew.MyIsif;

import come.tool.map.JMap;

/**
 * 坐标  任我行
 * @author Administrator
 *
 */
public class Mapmodelworld {
    // private double map_Screen_x;
    // private double map_Screen_y;
    // private double Screen_x;//人在屏幕的位置
    // private double Screen_y;
    private int w;
    private int h;
    private int bottom;
    private int right;
    private String mappath;
    private List<Door> doors = new ArrayList<>();
    private int min_x;
    private int min_y;
    // 小地图和大地图的比例
    private double bili_x;
    private double bili_y;
    private JMap jMap;
    /** 用于寻路节点 */
    private byte[][] Nodes;
    /** 地图参数 */
    private GamemapWorld gamemap;
    /** 小地图 */
    private ImgZoom zoom;

    public Mapmodelworld() {
        // TODO Auto-generated constructor stub
    }

    /**
     * 重置小地图参数
     */
    public void MiniMap(Image image) {
        getZoom().middleimg(image);
        //TestsmallmapJframe.getTestsmallmapJframe().getTestsmallmapJpanel().getOffBtn().setBounds(Util.mapmodel.getZoom().getMiddlew() + 25, 0, 25, 25);
        //       更新小地图方法
        min_x = image.getWidth(null) - 85;
        min_y = image.getHeight(null) - 60;
        bili_x = (double) w / min_x;
        bili_y = (double) h / min_y;
    }

    /**
     * 重置地图大小
     */
    public void Mapsize() {
        Dimension mapsize = jMap.getMapSize();
        right = mapsize.width;
        bottom = mapsize.height;
        w = right / Util.CELL_WIDTH;
        h = bottom / Util.CELL_WIDTH;
    }

    // 初始化地图属性
    public Mapmodelworld(double map_Screen_x, double map_Screen_y, JMap jMap, GamemapWorld gamemap) {
        super();
        this.gamemap = gamemap;
        this.jMap = jMap;
        Dimension mapsize = jMap.getMapSize();
        right = mapsize.width;
        bottom = mapsize.height;
        w = right / Util.CELL_WIDTH;
        h = bottom / Util.CELL_WIDTH;
        this.Nodes = new byte[w][h];
    }

    /** 判断人物位置是否处于遮罩 */
    public boolean zhezhao(int x, int y) {
        x = (x + shot_x - ScrenceUntil.Screen_x / 2) / Util.CELL_WIDTH;
        y = (y + shot_y - ScrenceUntil.Screen_y / 2) / Util.CELL_WIDTH;
        if (jMap.getMaprules()[y][x] == 2) {
            return true;
        }
        return false;
    }

    //TODO 切换地图，走到矩阵内如果没有切换地图说明当前的触点属于障碍触点 != 3,需要调整矩阵---自动寻路过图点取矩阵 (x1+x2)/2,(y1+y2)/2
    /** 判断人物位置是否处于tp点 */
    public Door tp(int x, int y) {
        if (jMap.getMaprules()[y / Util.CELL_WIDTH][x / Util.CELL_WIDTH] == 3) {
            Door door = Seekdoor(x, y);
            return door;
        }
        return null;
    }

    /**
     * 绘制地图
     */
    public void drawmap(Graphics g) {
        if (jMap != null) {
            jMap.draw(g, shot_x - ScrenceUntil.Screen_x / 2, shot_y - ScrenceUntil.Screen_y / 2);
        }

    }

    /**
     * 判断是否为障碍物 true为障碍物
     */
    public boolean mskpanduan(int x, int y) {
        return jMap.getMaprules()[y][x] == 0;
    }

    /** 寻找矩阵 找不到矩阵就直接用在250范围的内第一个矩阵 */
    public Door Seekdoor(int x, int y) {
        Door door = null;
        int p = 1000;
        for (int i = 0; i < doors.size(); i++) {
            int a = matchingdoor(doors.get(i), x, y);
            if (a < p && a < 250) {
                p = a;
                door = doors.get(i);
            }
        }
        return door;
    }

    /**
     * 匹配矩阵 匹配的矩阵返回0 不匹配的返回大于200的值 疑似的返回1-200
     */
    public int matchingdoor(Door door, int x, int y) {
        if (door == null) {
            return 1001;
        }
        int[] rect = door.getRects();
        if (rect == null) {
            return 1001;
        }
        return (Math.abs(Math.abs((rect[0] + rect[1]) / 2) - x) + Math.abs(Math.abs((rect[2] + rect[3]) / 2) - y)) / 2;
    }

    /** 判断是否超出屏幕的范围 若没超出返回在屏幕的位置 */
    PathPoint point = new PathPoint(0, 0);

    public PathPoint path(int x, int y) {
        if (Math.abs(x - shot_x) < ScrenceUntil.Screen_x / 2 && Math.abs(y - shot_y) < ScrenceUntil.Screen_y / 2) {
            point.setX(x - shot_x + ScrenceUntil.Screen_x / 2);
            point.setY(y - shot_y + ScrenceUntil.Screen_y / 2);
            return point;
        }
        return null;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getBottom() {
        return bottom;
    }

    public void setBottom(int bottom) {
        this.bottom = bottom;
    }

    public int getRight() {
        return right;
    }

    public void setRight(int right) {
        this.right = right;
    }

    public String getMappath() {
        return mappath;
    }

    public void setMappath(String mappath) {
        this.mappath = mappath;
    }

    public List<Door> getDoors() {

        return doors;
    }

    public void setDoors(List<Door> doors) {
        this.doors = null;
        this.doors = doors;
    }

    public int getMin_x() {
        return min_x;
    }

    public void setMin_x(int min_x) {
        this.min_x = min_x;
    }

    public int getMin_y() {
        return min_y;
    }

    public void setMin_y(int min_y) {
        this.min_y = min_y;
    }

    public double getBili_x() {
        return bili_x;
    }

    public void setBili_x(double bili_x) {
        this.bili_x = bili_x;
    }

    public double getBili_y() {
        return bili_y;
    }

    public void setBili_y(double bili_y) {
        this.bili_y = bili_y;
    }

    public GamemapWorld getGamemapWorld() {
        return gamemap;
    }

    public void setGamemapWorld(GamemapWorld gamemap) {
        this.gamemap = gamemap;
    }

    public byte[][] getNodes() {
        if (Nodes == null || Nodes.length != jMap.getMaprules().length
                || Nodes[0].length != jMap.getMaprules()[0].length) {
            Nodes = new byte[jMap.getMaprules()[0].length][jMap.getMaprules().length];
        } else {
            for (int i = 0; i < Nodes.length; i++) {
                for (int k = 0; k < Nodes[0].length; k++) {
                    Nodes[k][i] = 0;
                }
            }
        }
        return Nodes;
    }

    public void setNodes(byte[][] nodes) {
        Nodes = nodes;
    }

    public JMap getjMap() {
        if (jMap == null)
            jMap = new JMap();
        return jMap;
    }

    public void setjMap(JMap jMap) {
        this.jMap = jMap;
    }

    // 镜头
    private int shot_x;
    private int shot_y;
    // 镜头内活动区域大小
    private static int LIMIT_X = 0;
    private static int LIMIT_Y = 0;
    // 镜头内活动区域大小
    //private static int LIMIT_X = 70;
    //private static int LIMIT_Y = 25;
    // 人物历史位置
    private int history_x;
    private int history_y;

    // public void ce(){
    // shot_x+=3;
    // shot_y+=Util.random.nextInt(3);
    // }
    /**
     * 镜头滑动
     */
    public void ShotMove(int x, int y) {
        ShotMove_X(x);
        ShotMove_Y(y);
        history_x = x;
        history_y = y;
    }

    /**
     * x方向镜头移动
     */
    public void ShotMove_X(int x) {

        if (x == history_x && x == shot_x)
            return;
        if (x != history_x)
            setShot_x(x);
    }

    /**
     * y方向镜头移动
     */
    public void ShotMove_Y(int y) {
        if (y == history_y && y == shot_y)
            return;
        if (y != history_y)
            setShot_y(y);
    }

    public void setShot_x(int shot_x) {
//         this.shot_x-=(int) ((history_x-shot_x)*0.6);
        int ban = ScrenceUntil.Screen_x / 2;
        if (Math.abs(this.shot_x - shot_x) > LIMIT_X)
            this.shot_x = shot_x + (shot_x - this.shot_x < 0 ? +LIMIT_X : -LIMIT_X);
        if (this.shot_x > jMap.getMapWidth() - ban)
            this.shot_x = jMap.getMapWidth() - ban;
        if (this.shot_x < ban)
            this.shot_x = ban;

    }

    public void setShot_y(int shot_y) {
//         this.shot_y-=(int) ((history_y-shot_y)*0.6);
        int ban = ScrenceUntil.Screen_y / 2;
        if (Math.abs(this.shot_y - shot_y) > LIMIT_Y)
            this.shot_y = shot_y + (shot_y - this.shot_y < 0 ? +LIMIT_Y : -LIMIT_Y);
        if (this.shot_y > jMap.getMapHeight() - ban)
            this.shot_y = jMap.getMapHeight() - ban;
        if (this.shot_y < ban)
            this.shot_y = ban;

    }

    /**
     * 镜头回滚
     *
     * @return
     */
    public void ShotMiddlex(int x) {
        if (x == history_x && x == shot_x)
            return;
        shot_x += (x - shot_x > 0 ? 1 : -1) * (Math.abs(x - shot_x) > 2 ? 2 : 1);
        int banx = ScrenceUntil.Screen_x / 2;
        if (shot_x > jMap.getMapWidth() - banx)
            shot_x = jMap.getMapWidth() - banx;
        if (shot_x < banx)
            shot_x = banx;
        history_x = x;
    }

    public void ShotMiddley(int y) {
        if (y == history_y && y == shot_y)
            return;
        shot_y += (y - shot_y > 0 ? 1 : -1) * (Math.abs(y - shot_y) > 2 ? 2 : 1);
        int bany = ScrenceUntil.Screen_y / 2;
        if (this.shot_y > jMap.getMapHeight() - bany)
            this.shot_y = jMap.getMapHeight() - bany;
        if (this.shot_y < bany)
            this.shot_y = bany;
        history_y = y;
    }

    /**
     * 镜头强制回归
     *
     * @return
     */
    public void force(int x, int y) {
        shot_x = x;
        history_x = x;
        shot_y = y;
        history_y = y;
        int banx = ScrenceUntil.Screen_x / 2;
        if (shot_x > jMap.getMapWidth() - banx)
            shot_x = jMap.getMapWidth() - banx;
        if (shot_x < banx)
            shot_x = banx;
        int bany = ScrenceUntil.Screen_y / 2;
        if (this.shot_y > jMap.getMapHeight() - bany)
            this.shot_y = jMap.getMapHeight() - bany;
        if (this.shot_y < bany)
            this.shot_y = bany;
    }

    public int getShot_x() {
        return shot_x - ScrenceUntil.Screen_x / 2;
    }

    public int getShot_y() {
        return shot_y - ScrenceUntil.Screen_y / 2;
    }

    public ImgZoom getZoom() {
        if (zoom == null) {
            this.zoom = CutButtonImage.cuts("inkImg/old/background/49.png", 1, 1, false);
//        	this.zoom = CutButtonImage.cuts("inkImg/old/9/new/smp/smp.png", 1, 35, false);
        }
        return zoom;
    }

    public void setZoom(ImgZoom zoom) {
        this.zoom = zoom;
    }
}
